
#include <stdio.h>
#include <malloc.h>
#include <pthread.h>
#include <sched.h>
#include <stdbool.h>
#include <sys/time.h>
#include "../Include/shm_common.h"
#include <stdlib.h>
#define NUM_IT 12
pthread_t t1,t2,t3;

//per statistiche
queue_t * queue;
#define SIZE 13
    int vect[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
void *thread_uno(void *arg)
{
    int i;
    pthread_t id=pthread_self();
    for(i=0;i<NUM_IT;i++)
    {

        printf("Thread uno: sleep...\n");
        //sleep(2);
        printf("Thread uno: inserisco \n");
        enqueue(queue,(void *)(&(vect[i%SIZE]))); //chiaramente cambiera'...e' solo per test
        printf("Actual size: %d\n",getActualSize(queue));

    }
    return 0;

}

void *thread_due(void *arg)
{
    int i;
    pthread_t id=pthread_self();
    for(i=0;i<NUM_IT;i++)
    {
        printf("[Thread 2] estraggo...\n");
        int *ext=(int *)dequeue(queue);
        printf("Thread 2: estratto %d\n",*ext);
        printf("Actual size: %d\n",getActualSize(queue));
        //sleep(1);
    }
    return 0;
}
int main(int argc, char *argv[])
{

    void *status;
    struct timeval tmp_t;
    long start_tmill,end_tmill;

    queue=createQueue(10);

    cpu_set_t mask; //la maschera
    CPU_ZERO(&mask);
    CPU_SET(0, &mask); //il primo thread va sul primo core
    gettimeofday(&tmp_t,NULL);
    start_tmill=tmp_t.tv_sec*1000+(tmp_t.tv_usec/1000);
    pthread_create(&t1,NULL,thread_uno,NULL);
    pthread_setaffinity_np(t1,sizeof(cpu_set_t),&mask);

    CPU_ZERO(&mask);
    CPU_SET(2, &mask); //il secondo thread va sul secondo core
    pthread_create(&t2,NULL,thread_due,NULL);
    pthread_setaffinity_np(t2,sizeof(cpu_set_t),&mask);

    pthread_join(t1,&status);
    pthread_join(t2,&status);
    //PERFSTOP
      gettimeofday(&tmp_t,NULL);
    end_tmill=tmp_t.tv_sec*1000+(tmp_t.tv_usec/1000)-start_tmill;

    printf("gettimeofday: time elapsed (msec): %ld\n",end_tmill);


    return 0;

}

